/*
 * Copyright (c) 2013, Frank-Li.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer in the documentation and/or other materials provided
 *    with the distribution.
 * 3. The name of the author may not be used to endorse or promote
 *    products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This file is part of the Contiki OS
 *
 * $Id: contiki-main.c,v 1.2 2010/10/27 14:05:24 salvopitru Exp $
 */
/*---------------------------------------------------------------------------*/
/**
* \file
*			Contiki main file.
* \author
*			Frank Li <lgl88911@163.com>
*/
/*---------------------------------------------------------------------------*/

#include <stdio.h>         
#include "contiki.h"
#include "DrvUART.h"
#include "DrvGPIO.h"
#include "DrvSYS.h"
#include "DrvTimer.h"

void UART_INT_HANDLE(uint32_t u32IntStatus)
{
	uint8_t u8InChar;


	if(u32IntStatus & DRVUART_RDAINT)
	{
		/* Get all the input characters */
		while(UART0->ISR.RDA_IF==1)
		{
			/* Get the character from UART Buffer */
			DrvUART_Read(UART_PORT0, &u8InChar,1);
			//printf("read char %x\n\r", u8InChar);
			DrvUART_Write(UART_PORT0, &u8InChar,1);
			serial_line_input_byte(u8InChar);
		}
	}
}


/*---------------------------------------------------------------------------*/
int
main(void)
{
  
  /*
   * Initalize hardware.
   */
	STR_UART_T param;
	uint8_t u8Option;

	/* Unlock the protected registers */
	UNLOCKREG();

	/* Enable the 12MHz oscillator oscillation */
	DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1);

	/* Waiting for 12MHz Xtal stable */
	DrvSYS_Delay(5000);

	/* HCLK clock source. 0: external 12MHz. */
	DrvSYS_SelectHCLKSource(0);

	LOCKREG();

	DrvSYS_SetClockDivider(E_SYS_HCLK_DIV, 0); /* HCLK clock frequency = HCLK clock source / (HCLK_N + 1) */

	DrvGPIO_InitFunction(E_FUNC_UART0);

	/* Select UART Clock Source From 12MHz */
	DrvSYS_SelectIPClockSource(E_SYS_UART_CLKSRC,0);

	param.u32BaudRate        = 115200;
	param.u8cDataBits        = DRVUART_DATABITS_8;
	param.u8cStopBits        = DRVUART_STOPBITS_1;
	param.u8cParity          = DRVUART_PARITY_NONE;
	param.u8cRxTriggerLevel  = DRVUART_FIFO_1BYTES;
	param.u8TimeOut          = 0;
	DrvUART_Open(UART_PORT0, &param);

	DrvUART_EnableInt(UART_PORT0,   DRVUART_RDAINT, UART_INT_HANDLE);

	DrvTIMER_Init();
	DrvSYS_SelectIPClockSource(E_SYS_TMR0_CLKSRC, 0);
	DrvTIMER_Open(E_TMR0, 1000, E_PERIODIC_MODE);
	DrvTIMER_EnableInt(E_TMR0);
	DrvTIMER_Start(E_TMR0);

	DrvTIMER_Delay(E_TMR0, 200);
	printf("Hello world test\n\r");

	Sr04_init();
	DH11_Init();


/*
	while(1)
	{

		DrvTIMER_Delay(E_TMR0, 10);
		printf("Num %d\n\r", DrvTIMER_GetIntTicks(E_TMR0));
		//Nrf24_Check();
		//DrvUART_Write(UART_PORT0,  (uint8_t *)"Hello Word !\r\n", sizeof("Hello Word !\r\n")-1);
	}
*/	

  /*
   * Initialize Contiki and our processes.
   */
	clock_init();
	process_init();
	serial_line_init();
  
	rtimer_init();
  /* etimer_process should be initialized before ctimer */
  process_start(&etimer_process, NULL);  
  ctimer_init();
  netstack_init();

	procinit_init();

	autostart_start(autostart_processes);

	while(1) {
    
		int r;
		do {
			/* Reset watchdog. */
			r = process_run();
		} while(r > 0);
	}
  
}

